<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of imagem_controller
 *
 * @author link2
 */
class Img extends CMS_Controller {

    //put your code here

    public function __construct() {
        parent::__construct();
        
        $this->load->library('image_lib');

        $this->load->model('imagens/imagem_model');

        $this->load->helper(array('file', 'encode'));
        $this->load->config('cms');
        $this->load->helper('html');
    }

    public function get($id) {
        $_b_consultar = $this->imagem_model->get($id);
        if ($_b_consultar != null) {
            $_img = url_base64_decode($_b_consultar->imagem);
            header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
            header('Content-Type: ' . $_b_consultar->tipo);
            header('Content-Length: ' . strlen(trim($_img)));
            echo $_img;
        } else {
            $width = 50;
            $height = 16;
            $image = imagecreatetruecolor($width, $height);
            $color = imagecolorallocate($image, 255, 255, 255);
            imagefilledrectangle($image, 0, 0, $width, $height, $color);
            header('Content-Type: image/jpg');
            imagejpeg($image);
            imagedestroy($image);
        }
    }
    
    public function save($id){
        //http://pixlr.com/express/?s=c&image=http%3A//developer.pixlr.com/_image/example3.jpg&title=Example%20image%203&target=http%3A//developer.pixlr.com/save_post.php&exit=http%3A//developer.pixlr.com&method=post
       
        $image_raw= file_get_contents($_FILES['image']['tmp_name']);
        header('Content-type:' . " image/jpeg");
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
        header('Content-Length: ' . strlen(trim($image_raw)));
       //$image = imagecreatefromstring(url_base64_encode($image_raw));
       //$width = imagesx($image);
       //$height = imagesy($image);
       //imagejpeg($image,NULL, 85);
       echo $image_raw;
       
       /// IMPRIMIR SUBSTITUIR NOVA IMAGEM OU SALVAR COMO NOVA IMAGEM?
       //////// PEGAR O ID QUE VEM NO URL
       //////////// INSERT ou UPDATE
       
       //imagedestroy($image);
       
       //$thumb = imagecreatetruecolor($width, $height);
        //pre($this->input->post("Upload"));
       // pre($_FILES['image']['name']);
        
        //pre(file_get_contents($_FILES['image']));
        //echo var_dump(file_get_contents($_FILES['image']['tmp_name']));
    }
  
    
    //$data = file_get_contents($_FILES['file']['tmp_name']);
//$image = imagecreatefromstring($data);
//header('Content-Type: image/png');
//imagepng($image);
//imagedestroy($image);
    
    

    /* $_id id da imagem do BD
     * $_q qualidade da imagem de 0 a 100
     * $_w largura da nova imagem
     * $_h altura da nova imagem
     * $_nome da imagem para facilitar o URL
     */
    public function thumb($_id, $_q, $_w, $_h = "", $_nome = "") {

        $max_width = $_w;

        //Get the imagem from DB
        $b_consultar = $this->imagem_model->get($_id);
        
        if($b_consultar == NULL){
            $width = 250;
            $height = 250;
            $image = imagecreatetruecolor($width, $height);
            $color = imagecolorallocate($image, 255, 255, 255);
            $colorText = imagecolorallocate($image, 220, 220, 219);
            imagefilledrectangle($image, 0, 0, $width, $height, $color);
            imagestring($image,3,10,10,"Ops! Sem imagem pra Carregar.",$colorText);
            header('Content-Type: image/jpg');
            imagejpeg($image);
            imagedestroy($image);
            
            
            
        }else{

            // Set the header
            header('Content-type:' . $b_consultar->tipo);

            // Get original size of image
            $image = imagecreatefromstring(url_base64_decode($b_consultar->imagem));
            $current_width = imagesx($image);
            $current_height = imagesy($image);

            // Set thumbnail width
            $widths = array($current_width, $max_width);
            $new_width = min($widths);

            // Calculate thumbnail height from given width to maintain ratio
            $new_height = $current_height / $current_width * $new_width;

            // Create new image using thumbnail sizes
            $thumb = imagecreatetruecolor($new_width, $new_height);

            // Copy original image to thumbnail
            imagecopyresampled($thumb, $image, 0, 0, 0, 0, $new_width, $new_height, imagesx($image), imagesy($image));

            // Show thumbnail on screen
            $show = imagejpeg($thumb,NULL, $_q);


            // Clean memory
            imagedestroy($image);
            imagedestroy($thumb);
        }
    }
}
